package com.rundatop.wx.controller;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.StringUtils;
import com.rundatop.core.util.JacksonUtil;
import com.rundatop.core.util.ResponseUtil;
import com.rundatop.dao.model.User;
import com.rundatop.wx.config.WxProperties;
import com.rundatop.wx.service.UserService;
import com.rundatop.wx.service.UserTokenManager;
import com.rundatop.wx.service.impl.WxService;
import com.rundatop.wx.util.WxUtil;
import com.rundatop.wx.wechat.WxAuthService;
import com.rundatop.wx.wechat.req.WeChatUserInfo;
import com.rundatop.wx.wechat.utils.WxHelper;


/**
 * jssdk
 */
@RestController
@RequestMapping("/wx/jssdk")
public class WxJssdkController extends BaseController {
    private final Log logger = LogFactory.getLog(WxJssdkController.class);

    @Autowired
    private WxService wxJssdkService;
    @Autowired
	private HttpServletRequest request;
    @Autowired
	private WxAuthService wxAuthService;
	@Autowired
	private WxProperties wxProperties;
	@Autowired
    private UserService userService;
	
    /**
     *@Author: hdx
     *@CreateTime: 20:39 2020/2/14
     *@param:  shareUrl 分享url地址
     *@Description: 初始化微信JSSDK Config信息
      1.先通过appId和appSecret参数请求指定微信地址 获取AccessToken
      2.在通过第一步中的AccessToken作为参数请求微信地址 获取jsapi_ticket临时票据(此处不考虑调用频率，使用者根据情况放入缓存或定时任务)
      3.通过第二步的JssdkGetticket和timestamp,nonceStr,url作为参数请求微信地址，获取签名signature
      4.将第三步获得的signature和jsapi_ticket,nonceStr,timestamp,url返回给前端，作为Config初始化验证的信息
     */
     @PostMapping("/initWXJSSDKConfigInfo")
     @CrossOrigin     
     public Object initWXJSConfig (@RequestBody(required = false) String body) throws Exception {
    	 String url = JacksonUtil.parseString(body, "url");
         logger.info("url=" + url);
         System.out.println(url);
         String json = "";
         if(StringUtils.isEmpty(url)) {
        	 return ResponseUtil.badArgument();
         }
         try {
             Map map = wxJssdkService.initJSSDKConfig(url);
             json = WxUtil.mapToJson(map);
         }catch (Exception e){
        	return ResponseUtil.badArgument();
         }
         return ResponseUtil.ok(json);
     }
     
 	
}
